package com.itHaipeng.web;

import com.alibaba.fastjson.JSON;
import com.itHaipeng.pojo.Brand;
import com.itHaipeng.service.BrandService;
import com.itHaipeng.service.impl.BrandServiceImpl;
import com.itHaipeng.util.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends SelfServlet{
    private BrandService brandService=new BrandServiceImpl();//利用多态
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");
        List<Brand> brands = brandService.selectAll();
        String jsonString = JSON.toJSONString(brands);
        if(jsonString!=null){
            response.getWriter().write(jsonString);
        }
    }
    public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        //接收品牌提交过来的数据
        BufferedReader requestReader = request.getReader();
        String s = requestReader.readLine();
        Brand brand = JSON.parseObject(s, Brand.class);
        if(brand.toString()!=null){
            brandService.insert(brand);
            response.getWriter().write("success");
        }
    }
    public void deleteById(HttpServletRequest request,HttpServletResponse response)throws Exception{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=utf-8");
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        if(s!=null){
            int[] ids = JSON.parseObject(s, int[].class);
            brandService.deleteById(ids);
            response.getWriter().write("success");
        }else {
            response.getWriter().write("删除失败");
        }
    }
    public void selectByPage(HttpServletRequest request,HttpServletResponse response)throws Exception{
        //接收前端传来的数据
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=utf-8");
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        int _page=Integer.parseInt(page);
        int _size=Integer.parseInt(size);
        PageBean<Brand> pageBean = brandService.selectByPage(_page, _size);
            //转json
            String jsonString = JSON.toJSONString(pageBean);
        System.out.println(jsonString);
            response.getWriter().write(jsonString);
    }
    public void selectByPageCondition(HttpServletRequest request,HttpServletResponse response)throws Exception{
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=utf-8");
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        int _page=Integer.parseInt(page);
        int _size=Integer.parseInt(size);
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        Brand brand = JSON.parseObject(s, Brand.class);
        //获取请求的条件
        PageBean<Brand> pageBean = brandService.selectByPageCondition(_page, _size, brand);
        //转json
        String jsonString = JSON.toJSONString(pageBean);
        System.out.println(jsonString);
        response.getWriter().write(jsonString);
    }
}
